
load('summary_data/tableA4_A5.rda')

ds = bind_cols(split(ds, ~ cycle))[,c(2, 3:5, 8:10, 13:15)]
rs = bind_cols(split(rs, ~ cycle))[,c(2, 3:5, 8:10, 13:15)]

names(ds) = names(rs) = c('Contribution Amount', 'CL1', 'L21', 'CL-L21', 'CL2', 'L22', 'CL-L22', 'CL3', 'L23', 'CL-L23')

tableA4 <- gt(ds, rowname_col = 1) |>
  fmt_number(decimals = 2) |>
  tab_header(title = "Match Rates, by Candidacy and Contribution Amount") |>
  tab_spanner(label = "Obama", columns = 2:4)  |>
  tab_spanner(label = "Clinton", columns = 5:7)  |>
  tab_spanner(label = "Biden", columns = 8:10) |>
  cols_label(
    "CL1"= html("CL"),
    "L21" = html("L2"),
    "CL-L21" = html("CL-L2"),
    "CL2"= html("CL"),
    "L22" = html("L2"),
    "CL-L22" = html("CL-L2"),
    "CL3"= html("CL"),
    "L23" = html("L2"),
    "CL-L23" = html("CL-L2")
  )|>
  tab_stubhead(label = "Contribution Amount") %>%
  as_latex()

as.character(tableA4) %>% write_lines('tables/tableA4.tex')

tableA5 <- gt(rs, rowname_col = 1) |>
  fmt_number(decimals = 2) |>
  tab_header(title = "Match Rates, by Candidacy and Contribution Amount") |>
  tab_spanner(label = "Romney", columns = 2:4)  |>
  tab_spanner(label = "Trump16", columns = 5:7)  |>
  tab_spanner(label = "Trump20", columns = 8:10) |>
  cols_label(
    "CL1"= html("CL"),
    "L21" = html("L2"),
    "CL-L21" = html("CL-L2"),
    "CL2"= html("CL"),
    "L22" = html("L2"),
    "CL-L22" = html("CL-L2"),
    "CL3"= html("CL"),
    "L23" = html("L2"),
    "CL-L23" = html("CL-L2")
  )|>
  tab_stubhead(label = "Contribution Amount") %>%
  as_latex()

as.character(tableA5) %>% write_lines('tables/tableA5.tex')
